import React from 'react';
import {BrowserRouter as Router, Route, Switch, Redirect} from 'react-router-dom';
import LoginPage from '../pages/login'
import Index from '../pages/index'
import Navbar from "../components/Navbar/index";
import EditNote from '../pages/notes/editnote';
import NoteList from '../pages/notes/notelist';
import AddNote from '../pages/notes/addnote';
import {connect} from 'react-redux'

 class MainRouter extends React.Component{

    render(){
        const PrivateRoute =({component: Component,...rest}) =>(
            <Route {...rest} render={props => (
                !!this.props.auth.token?  <Component {...props}/> : <Redirect to={`login`}/>
            )}/>
        );

        return(
            <Router>
                <div>
                <Navbar/>
                    <Switch>
                        <Route component={Index} exact path={`/index`}/>
                        <Route  exact path={`/login`} render={props =>(
                            !!this.props.auth.token? <Redirect to={`/index`} />: <LoginPage/>
                        )}/>
                        <Route component={EditNote} exact path={`/edit-note/:noteId`}/>
                        <Route component={AddNote} exact path={`/add-note`}/>
                        <Route component={NoteList} exact path={`/my-note/:page`}/>
                    </Switch>
                </div>
            </Router>
        )
    }
}

const mapStateToProps = ({auth}) =>({auth});
export default connect(mapStateToProps)(MainRouter)
